package org.springside.examples.showcase.utilities.wb;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;

import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.springside.modules.utils.RegexUtil;
import org.wltea.analyzer.lucene.IKTokenizer;

public class TokenUtils {
	private static StringBuilder result = new StringBuilder();

	public static synchronized String doSegment(String statement) {
		// 1.置空
		result.setLength(0);

		// 2.字符串初步过滤，目的很简单，尽可能去除干扰词
		String regex = "(<[^>]*>)";
		statement = statement.replaceAll(regex, " ");
		regex = "(http://([\\w|/|\\.]+))";
		List<String> matchGroupList = RegexUtil.getMatchGroupList(statement, regex, 1);
		if (matchGroupList != null && matchGroupList.size() > 0) {
			// for (String kw : matchGroupList) {
			// if (kw.length() > 1) {
			// result.append(kw.trim()).append(",");
			// }
			// }
			statement = statement.replaceAll(regex, " ");
		}
		regex = "(#(.*)#)";
		matchGroupList = RegexUtil.getMatchGroupList(statement, regex, 1);
		if (matchGroupList != null && matchGroupList.size() > 0) {
			for (String kw : matchGroupList) {
				kw = kw.replaceAll("[\\s@\\+\\-]*", "");
				// if (kw.length() > 1) {
				// result.append(kw.trim()).append(",");
				// }
			}
		}
		statement = statement.replaceAll("[!@#]", " ");
		// 3.引入分词器对处理过的字符串进行分词
		IKTokenizer tokenizer = new IKTokenizer(new StringReader(statement), false);
		try {
			while (tokenizer.incrementToken()) {
				TermAttribute termAtt = tokenizer.getAttribute(TermAttribute.class);
				if (termAtt.term().length() > 1) {
					result.append(termAtt.term().trim()).append(",");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	public static void main(String[] args) {
		String t = "#温州动车脱轨事故#事故发生后，浙江移动已启动应急通信保障机制，抢通了3个退服基站；并现场提供手机充电服务，以满足在场人员通信需要。@中国移动浙江公司提醒：如果有需要寻亲人、报平安、咨询如何献血、在现场需要帮助等，可拨打12580。http://t.cn/al9IZi";
		String result = TokenUtils.doSegment(t);
		String[] keywords = result.split(",");
		for (String kw : keywords) {
			System.out.println(kw);
		}
	}

}
